export default defineEventHandler(async (event) => {
  const query = getQuery(event);
  const { origin, destination, strategy = 0, waypoints } = query;
  
  // 高德地图API密钥
  const AMAP_KEY = 'ea14acfbeff3ab37b1a0ac893135a7e8';
  
  try {
    if (!origin || !destination) {
      throw createError({
        statusCode: 400,
        statusMessage: '缺少必要参数：origin 和 destination'
      });
    }
    
    // 构建查询参数
    const params = new URLSearchParams({
      key: AMAP_KEY,
      origin: origin as string,
      destination: destination as string,
      strategy: strategy as string,
      output: 'json',
      extensions: 'all'
    });
    
    if (waypoints) {
      params.append('waypoints', waypoints as string);
    }
    
    const response = await fetch(
      `https://restapi.amap.com/v3/direction/driving?${params.toString()}`
    );
    const routeData = await response.json();
    
    if (routeData.status === '1') {
      return {
        success: true,
        data: routeData.route,
        message: '路径规划成功'
      };
    } else {
      throw createError({
        statusCode: 500,
        statusMessage: `路径规划API错误: ${routeData.info}`
      });
    }
  } catch (error) {
    console.error('路径规划API调用失败:', error);
    throw createError({
      statusCode: 500,
      statusMessage: '路径规划失败'
    });
  }
}); 